<?php
/*
    OpenLib
    Copyright (C) 2011  Michael Gukov, Pham Thi Thuy Duong

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


namespace Sun;

abstract class View implements IView {
    protected $layer;
    protected $scene;

    protected $data;
    protected $cond;
    protected $err_data;

    public $data_scene;

    public function __construct($layer, $scene = null) {
        $this->layer = $layer;
        $this->scene = $scene;
        $this->err = null;
    }
    public function SetData($data = null, array $cond = array(), $err_data = null) {
        $this->data = $data;
        $this->err_data = $err_data;
        $this->cond = $cond;
        return $this;
    }
    public function Render() {
        if ($this->layer !== null)
            $this->RenderLayer($this->layer);
        else
            $this->RenderScene($this->scene);
    }

    public function RenderLayer($layer) {
        include "/app/layers/{$layer}.php";
    }

    public function RenderScene($scene) {
        $method = $scene."Scene";
        if (file_exists($file = get_class($this)."/{$scene}.php")) include $file;
        else /*if (method_exists($this, $method))*/ $this->$method();
        //else if (($parent = get_parent_class($this))) $parent::$method();
        //else throw new Exceptions\NotFound("File", $file);
        return $this;
    }

    public function RenderData() {
        $this->RenderScene($this->data_scene);
    }

    public function RenderIf($scene, $cond) {
        if (isset($this->cond[$cond])) $this->RenderScene($scene);
    }

    public function IncludeScene($name, $class = null) {
        if (empty($class)) $class = get_class($this);
        if (file_exists($file = $class."/{$name}.php")) {
            include $file;
        } else if (($parent = get_parent_class($class))){
            $this->IncludeScene($name, $parent);
        }
        else throw new Exceptions\NotFound("Scene", $name);
        return $this;
    }

    public function FindScene($name, $class = null) {
        if (empty($class)) $class = get_class($this);
        if (method_exists($this, $name."Scene")) return true;

        if (file_exists($class."/{$name}.php")) {
            return true;
        } else if (($parent = get_parent_class($class))){
            return $this->FindScene($name, $parent);
        }
        else return false;
    }

    public function __call($name, array $args) {
        $type = substr($name, -5);
        $name = substr($name, 0, -5);
        if ($type == "Scene") {
            $this->IncludeScene($name);
        } else throw new Exceptions\NotFound("File", $file);
    }
}
